﻿using System;
using System.Text;
using NetMicro.Core.Extensions;
using NetMicro.Core.Helper;
using NetMicro.EventBus.Messages;
using NetMicro.EventBus.Persistences;

namespace NetMicro.EventBus.Extensions
{
    /// <summary>
    /// 消息序列化扩展
    /// </summary>
    public static class MessageSerializerExtensions
    {
        /// <summary>
        /// 反序列化
        /// </summary>
        /// <param name="messageSerializer"></param>
        /// <param name="text"></param>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static T DeserializeE<T>(this IMessageSerializer messageSerializer, string text)
        {
            return (T)messageSerializer.Deserialize(text, typeof(T));
        }

        /// <summary>
        /// 反序列化
        /// </summary>
        /// <param name="messageSerializer"></param>
        /// <param name="bytes"></param>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static T DeserializeE<T>(this IMessageSerializer messageSerializer, byte[] bytes)
        {
            return (T)messageSerializer.Deserialize(Encoding.UTF8.GetString(bytes), typeof(T));
        }

        /// <summary>
        /// 序列化为bytes数组
        /// </summary>
        /// <param name="messageSerializer"></param>
        /// <param name="obj"></param>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static byte[] SerializeToBytesE<T>(this IMessageSerializer messageSerializer, T obj)
        {
            return Encoding.UTF8.GetBytes(messageSerializer.Serialize(obj));
        }

        /// <summary>
        /// 数据库消息转化为MQ传输消息
        /// </summary>
        /// <param name="messageSerializer"></param>
        /// <param name="messageStorage"></param>
        /// <returns></returns>
        public static MessageTransfer ConvertMessageTransfer(this IMessageSerializer messageSerializer, MessageStorageTransfer messageStorage)
        {
            return messageSerializer.DeserializeE<MessageTransfer>(messageStorage.EventBody);
        }

        /// <summary>
        /// MQ传输消息转为数据库入库消息
        /// </summary>
        /// <param name="messageSerializer"></param>
        /// <param name="messageStorage"></param>
        /// <returns></returns>
        public static MessageStorageTransfer ConvertMessageStorageTransfer(this IMessageSerializer messageSerializer, MessageTransfer messageStorage, TimeSpan delayAt)
        {
            return new MessageStorageTransfer
            {
                Id = SnowflakeHelper.GetLocalDriftId(),
                CreateTime = TimeSpan.Zero.GetTimestampSeconds(),
                DelayAt = delayAt.GetTimestampSeconds(),
                EventBody = messageSerializer.Serialize(messageStorage),
                ExpireTime = TimeSpan.FromDays(3).GetTimestampSeconds(),
                IsLocking = false,
                LockEnd = 0,
                RetryCount = messageStorage.RetryCount
            };
        }

        /// <summary>
        /// MQ传输消息转为数据库入库消息
        /// </summary>
        /// <param name="messageSerializer"></param>
        /// <param name="messageStorage"></param>
        /// <returns></returns>
        public static MessageStorageTransfer ConvertMessageStorageTransfer(this IMessageSerializer messageSerializer, MessageTransfer messageStorage)
        {
            return new MessageStorageTransfer
            {
                Id = SnowflakeHelper.GetLocalDriftId(),
                CreateTime = TimeSpan.Zero.GetTimestampSeconds(),
                EventBody = messageSerializer.Serialize(messageStorage),
                ExpireTime = TimeSpan.FromDays(3).GetTimestampSeconds(),
                IsLocking = false,
                LockEnd = 0,
                RetryCount = messageStorage.RetryCount,
                EventHandlerName = messageStorage.EventHandlerName,
                EventName = messageStorage.EventName,
                Exception = messageStorage.Exception,
                MsgId = messageStorage.MsgId,
                Version = messageStorage.Version
            };
        }
    }
}
